package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 获取用户订单分页信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        //获取用户订单分页
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据订单信息 倒序排列
        orderLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        //根据客户id 获取订单信息 条件
        orderLambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getThreadLocal());
        ordersService.page(ordersPage,orderLambdaQueryWrapper);
        //新建订单信息增强模型 用于完善订单数据
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //获取订单集合
        List<Orders> ordersPageRecords=ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders orders : ordersPageRecords) {
            OrdersDto ordersDto = new OrdersDto();
            //将订单号基础数据 赋值
            BeanUtils.copyProperties(orders,ordersDto);
            //根据订单号获取 菜品订单详细信息
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list=orderDetailService.list(orderDetailLambdaQueryWrapper);
            //为单笔订单设置订单号匹配的 详细订单信息
            ordersDto.setOrderDetails(list);
            ordersDtoList.add(ordersDto);
        }
        //将订单分页数据 赋值给dto
        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
        //设置 增强后的dto 给分页订单信息
        ordersDtoPage.setRecords(ordersDtoList);

        return R.success(ordersDtoPage);
    }

    /**
     * 商家后台获取所有订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, @DateTimeFormat(pattern="yy-MM-dd HH:mm:ss") Date beginTime,@DateTimeFormat(pattern="yy-MM-dd HH:mm:ss") Date endTime){
        //获取用户订单分页
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据订单信息 倒序排列
        orderLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        //设置根据订单号模糊查找订单 条件
        orderLambdaQueryWrapper.like(number!=null,Orders::getNumber,number);
        //设置日期条件
        //初始化LocalDateTime日期
        LocalDateTime localBeginTime = null;
        LocalDateTime localEndTime = null;
        if(beginTime !=null){
            //将Date类型转换成LocalDateTime类型
            localBeginTime =beginTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            localEndTime = endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
        orderLambdaQueryWrapper.between(beginTime!=null,Orders::getOrderTime,localBeginTime,localEndTime);
        ordersService.page(ordersPage,orderLambdaQueryWrapper);
        //新建订单信息增强模型 用于完善订单数据
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //获取订单集合
        List<Orders> ordersPageRecords=ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders orders : ordersPageRecords) {
            OrdersDto ordersDto = new OrdersDto();
            //将订单号基础数据 赋值
            BeanUtils.copyProperties(orders,ordersDto);
            //根据订单号获取 菜品订单详细信息
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list=orderDetailService.list(orderDetailLambdaQueryWrapper);
            //为单笔订单设置订单号匹配的 详细订单信息
            ordersDto.setOrderDetails(list);
            ordersDtoList.add(ordersDto);
        }
        //将订单分页数据 赋值给dto
        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
        //设置 增强后的dto 给分页订单信息
        ordersDtoPage.setRecords(ordersDtoList);

        return R.success(ordersDtoPage);

    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R changeOrderStatus(@RequestBody Orders orders){
        Orders orderById=ordersService.getById(orders.getId());
        //更改状态
        orderById.setStatus(orders.getStatus());
        //修改订单信息
        ordersService.updateById(orderById);

        return R.success("修改成功");
    }

    /**
     * 提交订单信息
     * @return
     */
    @PostMapping("submit")
    public R submit(@RequestBody Orders orders){
        Boolean flag = ordersService.submit(orders);
        return R.success("添加成功");
    }

    /**
     * 再来一单
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        //根据订单id 找到订单详细表
        Boolean flag = ordersService.again(orders);
        return R.success("再来一单");
    }

}
